package com.nineclock.system.controller;

import com.nineclock.common.entity.Result;
import com.nineclock.system.SysCompanyControllerApi;
import com.nineclock.system.dto.*;
import com.nineclock.system.service.SysCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/company")
public class SysCompanyController implements SysCompanyControllerApi {

    @Autowired
    SysCompanyService sysCompanyService;

    @Override
    @GetMapping
    public Result<SysCompanyDTO> queryCompanyInfo() {
        SysCompanyDTO sysCompanyDTO= sysCompanyService.queryCompanyInfo();
        return Result.success(sysCompanyDTO);
    }

    @Override
    @PostMapping("/uploadOSS")
    public Result<String> uploadCompanyLogo(MultipartFile file) throws IOException {
        String filePath=sysCompanyService.uploadCompanyLogo(file);
        return Result.success(filePath);
    }

    @Override
    @PutMapping
    public Result updateCompanyInfo(@RequestBody SysCompanyDTO sysCompanyDTO) {
        sysCompanyService.updateCompanyInfo(sysCompanyDTO);
        return Result.success();
    }

    @Override
    @GetMapping("/getCurrentAdmin")
    public Result<SysCompanyUserDTO> getCurrentAdmin() {
        SysCompanyUserDTO sysCompanyUserDTO=sysCompanyService.getCurrentAdmin();
        return Result.success(sysCompanyUserDTO);
    }

    @Override
    @PostMapping("/config/admin")
    public Result changeSysAdmin(@RequestBody SysChangeMainManagerDTO sysChangeMainManagerDTO) {
        sysCompanyService.changeSysAdmin(sysChangeMainManagerDTO);
        return Result.success();
    }

    @Override
    @GetMapping("/list")
    public Result<List<SysCompanyDTO>> queryCompanyByName(String keyword) {
        List<SysCompanyDTO> sysCompanyDTOS=sysCompanyService.queryCompanyByName(keyword);
        return Result.success(sysCompanyDTOS);
    }


    @Override
    @PostMapping("/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody SysApplyJoinCompanyUserDTO sysApplyJoinCompanyUserDTO) {
        sysCompanyService.applyJoinCompany(sysApplyJoinCompanyUserDTO);
        return Result.success();
    }

    @Override
    @PostMapping("/allowedJoinCompany")
    public Result allowedJoinCompany(SysAllowedJoinCompanyUserDTO sysAllowedJoinCompanyUserDTO) {
        sysCompanyService.allowedJoinCompany(sysAllowedJoinCompanyUserDTO);
        return Result.success();
    }

    @Override
    @GetMapping("/list/{userId}")
    public Result<List<SysCompanyDTO>> queryCompanyListByUserId(@PathVariable("userId") Long userId) {
        List<SysCompanyDTO> companyDTOList=sysCompanyService.queryCompanyListByUserId(userId);
        return Result.success(companyDTOList);
    }
}
